package edu.suda.LoopCodePlus.util.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import edu.suda.LoopCodePlus.util.JwtUtil;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@Component
public class JwtUtilImpl implements JwtUtil {
    private static final String SECRET = "CLittleTree";
    @Override
    public String createToken(String userId) {
        Calendar instance = Calendar.getInstance();
        // 48小时后令牌token失效
        instance.add(Calendar.HOUR,48);
        String token = JWT.create()
                .withClaim("userId", userId)  //payload
                .withExpiresAt(instance.getTime()) // 指定令牌的过期时间
                .sign(Algorithm.HMAC256(SECRET));//签名
        System.out.println(token);
        return token;
    }
    @Override
    public DecodedJWT verifyToken(String token) {
        return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }

}

